package com.example.glidedemo.transformation;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

public class GrayscaleTransformation extends BitmapTransformation {
    public GrayscaleTransformation() {
        super();
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {

        Bitmap.Config originalConfig = toTransform.getConfig();

        // 创建一个新的Bitmap，其颜色格式与原始Bitmap相同
        Bitmap bitmap = toTransform.copy(originalConfig, true);

        // 应用灰度效果
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);

        // 在新的Bitmap上绘制修改后的图像
        Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(originalConfig, true);
        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawBitmap(toTransform, 0, 0, paint);

        // 返回修改后的Bitmap
        return mutableBitmap;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }

    @Override
    public boolean equals(Object o) {
        return false;
    }

    @Override
    public int hashCode() {
        return 0;
    }
}